﻿using System;
using System.Collections.Generic;
using System.Windows.Media;
using System.Linq;
using Bling.Core;
using Bling.DSL;
using Bling.WPF;
using Bling.WPF.Custom;
using Bling.Graphics;
using Bling.Angles;

namespace Bling.Example {
  using Bling.Matrices;
  using Bling.WPF3D;
  using Bling.DX;
  using Bling.Util;
  public class Intro : HasDX10Canvas {
    public Intro() : base(true) {
      // Set position of render host.
      Host.LeftTop = 0d;
      // The size of render host has to be constant.
      Host.Size = this.Bl().Size;
      // sliders for scale and rotation.
      var Scale = new LabelSliderBl(OverlayContent) { LabelName = "Scale", Minimum = 0d, Maximum = 3d, Value = 1d, LeftTop = 0 };
      var Rotate = new LabelSliderBl(OverlayContent) { LabelName = "Rotate", Minimum = 0d, Maximum = 2d, Value = 0d, LeftTop = Scale.LeftBottom };
      // Create a simple index buffer.
      var indices = new List<int>();
      indices.Add(0, 1, 2);
      // render the triangle.
      Device.Render(3, indices, (IntBl n, IVertex vertex) => {
        var xy = new Double2Bl(n / 2, n % 2);
        // viewport is from [-1,-1] to [+1,+1], so center triangle by subtracting .5
        var xyz = new Double3Bl(xy - .5, 0);
        // compute center of triangle so we can rotate from center.
        var center = new Double2Bl(0, 0) + new Double2Bl(0, 1) + new Double2Bl(1, 0);
        center = center / 3d;
        // remember everything is displaced by .5
        center = center - .5;
        // a scale transform is simply the Scale slider's value swizzled to a 3D point (XXX), with our computed center (and a 0 Z coordinate)
        var transform = Matrix4Bl.MakeScale(Scale.Value, center.AddZ(0));
          
        // a quaternion for rotation, use the rotate slider's value as the angle (use PI() to multiply by PI and convert to angle). 
        var q = new QuaternionBl(new Double3Bl(0, 0, 1), Rotate.Value.PI());
        // compose the matrices, scale first so scale appears on the left (we are being row-major as in OpenGL).
        transform = q * transform;
        // transform the position
        xyz = transform.Transform(xyz);
        // set position
        vertex.Position = xyz;
        // set color as vertex-determined red, blue, or green.
        vertex.Color = new ColorBl[] {
          Colors.Red, Colors.Blue, Colors.Green
        }.Table(n);
      });
    }
  }
}
